﻿using ICSharpCode.AvalonEdit.Document;
using ICSharpCode.AvalonEdit.Folding;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Shapes;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor
{
    /// <summary>
    /// 此类用以定义编辑器的折叠区。
    /// </summary>
    class CustomFoldingStrategyForReporter : AbstractFoldingStrategy
    {
        private ICSharpCode.AvalonEdit.TextEditor edit;
        /// <summary>
        /// 要应用折叠功能的编辑器。
        /// </summary>
        public ICSharpCode.AvalonEdit.TextEditor Edit { get { return this.edit; } }

        /// <summary>
        /// [构造方法]创建一个新的折叠区。
        /// </summary>
        public CustomFoldingStrategyForReporter(ICSharpCode.AvalonEdit.TextEditor edit)
        {
            this.edit = edit;
        }

        /// <summary>
        /// 为特定文档创建 NewFolding 对象集。
        /// </summary>
        public override IEnumerable<NewFolding> CreateNewFoldings(TextDocument document, out int firstErrorOffset)
        {
            firstErrorOffset = -1;
            return CreateNewFoldings(document);
        }

        /// <summary>
        /// 为特定文档创建 NewFolding 对象集。
        /// </summary>
        public IEnumerable<NewFolding> CreateNewFoldings(ITextSource document)
        {
            List<NewFolding> newFoldings = new List<NewFolding>();
            var trimChars = new char[] { ' ', '　', '\t', };

            int currentLineOffset = -1;
            int currentLineEndOffset = -1;

            var regNewPageCommand = new Regex(@"^[；;][ 　\t]*新页面[ 　\t]*[：:]");
            DocumentLine prePageLineObject = null;

            var pageCount = 0;
            for (int i = 1; i <= edit.LineCount; i++)
            {
                var line = edit.Document.GetLineByNumber(i);
                currentLineOffset = line.Offset;
                currentLineEndOffset = line.EndOffset;
                string lineText = edit.Document.GetText(line.Offset, line.Length);

                if (string.IsNullOrEmpty(lineText)) continue;  // 空行忽略

                var match = regNewPageCommand.Match(lineText);
                if (match.Success)
                {
                    if (prePageLineObject == null)  // 第一个“；新页面：”命令行
                    {
                        prePageLineObject = line;
                        continue;
                    }
                    else
                    {
                        var newFolding = new NewFolding()
                        {
                            Name = $"新页面 {pageCount + 1}",
                            StartOffset = prePageLineObject.Offset,
                            EndOffset = currentLineOffset - 2,  // 这里一定是大于的
                        };
                        newFoldings.Add(newFolding);
                        prePageLineObject = line;
                    }

                    pageCount++;
                }
            }

            // 到编辑器末尾也算一个
            if (prePageLineObject != null && prePageLineObject.NextLine != null)
            {
                var lastLineObject = edit.Document.GetLineByNumber(edit.Document.LineCount);

                var newFolding = new NewFolding()
                {
                    Name = $"新页面 {pageCount + 1}",
                    StartOffset = prePageLineObject.Offset,
                    EndOffset = lastLineObject.EndOffset,  // 这里一定是大于的
                };
                newFoldings.Add(newFolding);
            }

            if (newFoldings.Count > 0)
            {
                newFoldings.Sort((a, b) => a.StartOffset.CompareTo(b.StartOffset));
            }

            return newFoldings;
        }
    }
}
